﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Catering_DBOpertion;
using System.Data;
using System.Data.SqlClient;

namespace Catering_Operation
{
    public partial class DailyRepairInfo
    {
        public DailyRepairInfo()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
            return DbHelperSQL.GetMaxID("id", "DailyRepairInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select count(1) from DailyRepairInfo");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
        public void Add(Catering_Model.DailyRepairInfo oMD)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("insert into DailyRepairInfo(");
            strSql.Append("DailyManagementID,GroupID,ClassID,SafeHiddenTroubleDesc,IsSafeEnough,RepairingDesc,BaoXiuDate,StartRepairDate,EndRepairDate,RepairedDesc,RepairUserState)");
			strSql.Append(" values (");
            strSql.Append("@DailyManagementID,@GroupID,@ClassID,@SafeHiddenTroubleDesc,@IsSafeEnough,@RepairingDesc,@BaoXiuDate,@StartRepairDate@EndRepairDate,@RepairedDesc,@RepairUserState)");
            SqlParameter[] prams = {
					new SqlParameter("@DailyManagementID", SqlDbType.Int, 4),
					new SqlParameter("@GroupID", SqlDbType.VarChar, 50),
					new SqlParameter("@ClassID", SqlDbType.VarChar, 50),
					new SqlParameter("@SafeHiddenTroubleDesc", SqlDbType.Text, 0),
					new SqlParameter("@IsSafeEnough", SqlDbType.Bit, 1),
					new SqlParameter("@RepairingDesc", SqlDbType.Text, 0),
					new SqlParameter("@BaoXiuDate", SqlDbType.DateTime, 18),
					new SqlParameter("@StartRepairDate", SqlDbType.DateTime, 18),
					new SqlParameter("@EndRepairDate", SqlDbType.DateTime, 18),
					new SqlParameter("@RepairedDesc", SqlDbType.Text, 0),
					new SqlParameter("@RepairUserState", SqlDbType.Int, 4)};
            prams[0].Value = oMD.DailyManagementID;
            prams[1].Value = oMD.GroupID != null ? oMD.GroupID : " ";
            prams[2].Value = oMD.ClassID != null ? oMD.ClassID : " ";
            prams[3].Value = oMD.SafeHiddenTroubleDesc != null ? oMD.SafeHiddenTroubleDesc : (object)DBNull.Value;
            prams[4].Value = oMD.IsSafeEnough;
            prams[5].Value = oMD.RepairingDesc != null ? oMD.RepairingDesc : " ";
            prams[6].Value = oMD.BaoXiuDate.Year > 1949 ? oMD.BaoXiuDate : (object)DBNull.Value;
            prams[7].Value = oMD.StartRepairDate.Year > 1949 ? oMD.StartRepairDate : (object)DBNull.Value;
            prams[8].Value = oMD.EndRepairDate.Year > 1949 ? oMD.EndRepairDate : (object)DBNull.Value;
            prams[9].Value = oMD.RepairedDesc != null ? oMD.RepairedDesc : (object)DBNull.Value;
            prams[10].Value = oMD.RepairUserState;
            DbHelperSQL.ExecuteSql(strSql.ToString(), prams);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(Catering_Model.DailyRepairInfo oMD)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("update DailyRepairInfo set ");
            strSql.Append("DailyManagementID=@DailyManagementID,");
			strSql.Append("GroupID=@GroupID,");
            strSql.Append("ClassID=@ClassID,");
            strSql.Append("SafeHiddenTroubleDesc=@SafeHiddenTroubleDesc,");
            strSql.Append("IsSafeEnough=@IsSafeEnough,");
            strSql.Append("RepairingDesc=@RepairingDesc,");
            strSql.Append("BaoXiuDate=@BaoXiuDate,");
            strSql.Append("StartRepairDate=@StartRepairDate,");
            strSql.Append("EndRepairDate=@EndRepairDate,");
            strSql.Append("RepairedDesc=@RepairedDesc,");
            strSql.Append("RepairUserState=@RepairUserState,");

			strSql.Append(" where id=@id ");
            SqlParameter[] prams = {
					new SqlParameter("@DailyManagementID", SqlDbType.Int, 4),
					new SqlParameter("@GroupID", SqlDbType.VarChar, 50),
					new SqlParameter("@ClassID", SqlDbType.VarChar, 50),
					new SqlParameter("@SafeHiddenTroubleDesc", SqlDbType.Text, 0),
					new SqlParameter("@IsSafeEnough", SqlDbType.Bit, 1),
					new SqlParameter("@RepairingDesc", SqlDbType.Text, 0),
					new SqlParameter("@BaoXiuDate", SqlDbType.DateTime, 18),
					new SqlParameter("@StartRepairDate", SqlDbType.DateTime, 18),
					new SqlParameter("@EndRepairDate", SqlDbType.DateTime, 18),
					new SqlParameter("@RepairedDesc", SqlDbType.Text, 0),
					new SqlParameter("@RepairUserState", SqlDbType.Int, 4)};
            prams[0].Value = oMD.DailyManagementID;
            prams[1].Value = oMD.GroupID != null ? oMD.GroupID : " ";
            prams[2].Value = oMD.ClassID != null ? oMD.ClassID : " ";
            prams[3].Value = oMD.SafeHiddenTroubleDesc != null ? oMD.SafeHiddenTroubleDesc : (object)DBNull.Value;
            prams[4].Value = oMD.IsSafeEnough;
            prams[5].Value = oMD.RepairingDesc != null ? oMD.RepairingDesc : " ";
            prams[6].Value = oMD.BaoXiuDate.Year > 1949 ? oMD.BaoXiuDate : (object)DBNull.Value;
            prams[7].Value = oMD.StartRepairDate.Year > 1949 ? oMD.StartRepairDate : (object)DBNull.Value;
            prams[8].Value = oMD.EndRepairDate.Year > 1949 ? oMD.EndRepairDate : (object)DBNull.Value;
            prams[9].Value = oMD.RepairedDesc != null ? oMD.RepairedDesc : (object)DBNull.Value;
            prams[10].Value = oMD.RepairUserState;
            prams[11].Value = oMD.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), prams);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("delete from DailyRepairInfo ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("delete from DailyRepairInfo ");
            strSql.Append(" where " + idlist);
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public Catering_Model.DailyRepairInfo GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 id,DailyManagementID,GroupID,ClassID,SafeHiddenTroubleDesc,IsSafeEnough,RepairingDesc,BaoXiuDate,StartRepairDate,EndRepairDate,RepairedDesc,RepairUserState from DailyRepairInfo ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = id;

            Catering_Model.DailyRepairInfo model = new Catering_Model.DailyRepairInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
                if (ds.Tables[0].Rows[0]["DailyManagementID"] != null && ds.Tables[0].Rows[0]["DailyManagementID"].ToString() != "")
				{
                    model.DailyManagementID = int.Parse(ds.Tables[0].Rows[0]["DailyManagementID"].ToString());
				}				
				if(ds.Tables[0].Rows[0]["GroupID"]!=null && ds.Tables[0].Rows[0]["GroupID"].ToString()!="")
				{
					model.GroupID=ds.Tables[0].Rows[0]["GroupID"].ToString();
				}
                if (ds.Tables[0].Rows[0]["ClassID"] != null && ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = ds.Tables[0].Rows[0]["ClassID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SafeHiddenTroubleDesc"] != null && ds.Tables[0].Rows[0]["SafeHiddenTroubleDesc"].ToString() != "")
				{
                    model.SafeHiddenTroubleDesc = ds.Tables[0].Rows[0]["SafeHiddenTroubleDesc"].ToString();
				}
                if (ds.Tables[0].Rows[0]["IsSafeEnough"] != null && ds.Tables[0].Rows[0]["IsSafeEnough"].ToString() != "")
				{
                    model.IsSafeEnough = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsSafeEnough"].ToString());
				}
                if (ds.Tables[0].Rows[0]["RepairingDesc"] != null && ds.Tables[0].Rows[0]["RepairingDesc"].ToString() != "")
				{
                    model.RepairingDesc = ds.Tables[0].Rows[0]["RepairingDesc"].ToString();
				}
                if (ds.Tables[0].Rows[0]["BaoXiuDate"] != null && ds.Tables[0].Rows[0]["BaoXiuDate"].ToString() != "")
				{
                    model.BaoXiuDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["BaoXiuDate"].ToString());
				}
                if (ds.Tables[0].Rows[0]["StartRepairDate"] != null && ds.Tables[0].Rows[0]["StartRepairDate"].ToString() != "")
				{
                    model.StartRepairDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["StartRepairDate"].ToString());
				}
                if (ds.Tables[0].Rows[0]["EndRepairDate"] != null && ds.Tables[0].Rows[0]["EndRepairDate"].ToString() != "")
                {
                    model.EndRepairDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["EndRepairDate"].ToString());
                }

                if (ds.Tables[0].Rows[0]["RepairedDesc"] != null && ds.Tables[0].Rows[0]["RepairedDesc"].ToString() != "")
                {
                    model.RepairedDesc = ds.Tables[0].Rows[0]["RepairedDesc"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RepairUserState"] != null && ds.Tables[0].Rows[0]["RepairUserState"].ToString() != "")
                {
                    model.RepairUserState = Convert.ToInt32(ds.Tables[0].Rows[0]["RepairUserState"].ToString());
                }  

				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select id,DailyManagementID,GroupID,ClassID,SafeHiddenTroubleDesc,IsSafeEnough,RepairingDesc,BaoXiuDate,StartRepairDate,EndRepairDate,RepairedDesc,RepairUserState ");
            strSql.Append(" FROM DailyRepairInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" id,DailyManagementID,GroupID,ClassID,SafeHiddenTroubleDesc,IsSafeEnough,RepairingDesc,BaoXiuDate,StartRepairDate,EndRepairDate,RepairedDesc,RepairUserState ");
            strSql.Append(" FROM DailyRepairInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "DailyRepairInfo";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("GetPageData", parameters, "DailyRepairInfo");
		}

        /// <summary>
        /// 根据条件获取数据总条数
        /// </summary>
        public int GetCountBywhere(string where)
        {
            string strSql = string.Format("select count(*) from DailyRepairInfo where {0}", where);
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql));
        }

		#endregion  Method
    }
}
